package com.example.demo.controller;

import com.example.demo.entity.Charges;
import com.example.demo.entity.Fee_list;
import com.example.demo.pojo.PlannerResult;
import com.example.demo.repository.ChargesRepository;
import com.example.demo.repository.Fee_listRepository;
import com.example.demo.vo.ChargesVo;
import com.example.demo.vo.Fee_listVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/fee")
public class feeListController {

    @Autowired
    Fee_listRepository fee_listRepository;

    @Autowired
    ChargesRepository chargesRepository;

    @PostMapping("/save")
    public PlannerResult<String> createcharges(@RequestBody ChargesVo chargesVo)
    {
        Charges charges=new Charges();
        charges.setActivity_id(chargesVo.getActivity_id());
        charges.setBooking_fee(chargesVo.getBooking_fee());
        charges.setActual_fee(0);
        Charges result=chargesRepository.save(charges);
        if(result.getId() !=null)
        {
            return PlannerResult.success("success");
        }
        else {
            return PlannerResult.error("error");
        }
    }


    @PostMapping("/update")
    public PlannerResult<String> updatecharges(@RequestBody ChargesVo chargesVo)
    {
        Charges charges=chargesRepository.findByActivityId(chargesVo.getActivity_id());
        if(!(charges==null)){
            BeanUtils.copyProperties(chargesVo,charges);
        }else{
            charges = new Charges();
           BeanUtils.copyProperties(chargesVo,charges);
        }
        Charges result=chargesRepository.save(charges);
        if(result.getId() !=null)
        {
            return PlannerResult.success("success");
        }
        else {
            return PlannerResult.error("error");
        }
    }

    @PostMapping("/fee")
    public PlannerResult<String> fee(Fee_listVo feeListVo)
    {
        Fee_list feeList = new Fee_list();
        feeList.setFee(feeListVo.getFee());
        feeList.setContext(feeListVo.getContext());
        feeList.setActivity_id(feeListVo.getActivity_id());
        Fee_list result = fee_listRepository.save(feeList);
        if(result.getId()!=null)
        {
            //更新活动实际费用
            Charges charges = chargesRepository.findByActivityId(feeListVo.getActivity_id());
            charges.setActual_fee(charges.getActual_fee()+feeListVo.getFee());
            return PlannerResult.success("费用清单添加成功");
        }
        else {
            return PlannerResult.error("费用清单添加失败");
        }
    }


    public class Money{
        public List<Fee_list> feemoney;
        public Charges  chargesmoney;
    }
    @GetMapping("/myfee")
    public PlannerResult<Money> my_fee(@RequestParam ("activityid") Integer activityid)
    {
        List<Fee_list> feeLists = fee_listRepository.findByActivity_id(activityid);
        Charges charges= chargesRepository.findByActivityId(activityid);
        Money money = new Money();
        money.chargesmoney.setActual_fee(charges.getActual_fee());
        money.chargesmoney.setBooking_fee(charges.getBooking_fee());
        money.chargesmoney.setId(charges.getId());
        money.chargesmoney.setActivity_id(charges.getActivity_id());
        for (Fee_list feelist: feeLists
        ) {
            money.feemoney.add(feelist);
        }
        return PlannerResult.success(money);
    }

    @GetMapping("/all")
    public PlannerResult<List<Fee_list>> feeList(@RequestParam("act_id" ) Integer act_id){
        return PlannerResult.success(fee_listRepository.findByActivity_id(act_id));
    }
}
